package com.qianfeng.gl4study.youshare.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.model.CollectItem;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/25
 * Email:pdsfgl@live.com
 */
public class CollectAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<CollectItem> CollectItems;
	//设置外部事件处理监听器，用于处理增加，减少，删除的操作。
	// 设置成成员变量的目的是为了让外部监听器计算总金额
	private View.OnClickListener operationListener;
	/**
	 * 用于处理购物车项目的选中与未选中状态
	 * 进行计算总金额
	 */
	private CompoundButton.OnCheckedChangeListener checkedChangeListener;
	//购买数量文本变化观察者
	private TextWatcher textWatcher;


	public CollectAdapter(Context context,List<CollectItem> items) {
		if(context!=null){
			inflater = LayoutInflater.from(context);
		}else {
			throw new IllegalArgumentException("context must not null");
		}
		CollectItems = items;
	}

	public void setCheckedChangeListener(CompoundButton.OnCheckedChangeListener checkedChangeListener) {
		this.checkedChangeListener = checkedChangeListener;
	}

	public void setOperationListener(View.OnClickListener operationListener) {
		this.operationListener = operationListener;
	}

	/**
	 * 设置购物车中每一项的购买数量的文本变化监听
	 * @param textWatcher
	 */
	public void setTextWatcher(TextWatcher textWatcher) {
		this.textWatcher = textWatcher;
	}

	@Override
	public int getCount() {
		int ret = 0;
		if(CollectItems!=null){
			ret = CollectItems.size();
		}
		return ret;
	}

	@Override
	public Object getItem(int position) {
		Object ret = null;
		if(CollectItems!=null){
			ret = CollectItems.get(position);
		}
		return ret;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if(convertView!=null){
			view = convertView;
		}else {
			view = inflater.inflate(R.layout.item_collect,parent,false);
		}
		ViewHolder holder = (ViewHolder)view.getTag();
		if(holder==null){
			holder = new ViewHolder();
			holder.checkBox = (CheckBox)view.findViewById(R.id.item_cart_cheked);
			holder.imaIcon = (ImageView)view.findViewById(R.id.item_cart_icon);
			holder.txtTitle = (TextView)view.findViewById(R.id.item_cart_title);
			holder.txtPrice = (TextView)view.findViewById(R.id.item_cart_price);
			holder.txtCount = (TextView)view.findViewById(R.id.item_cart_count);
			holder.txtDelect = (TextView)view.findViewById(R.id.item_cart_delete);
			holder.btnInc = (Button)view.findViewById(R.id.item_cart_add);
			holder.btnDec = (Button)view.findViewById(R.id.item_cart_sub);

			if(operationListener!=null){
				holder.btnInc.setOnClickListener(operationListener);
				holder.btnDec.setOnClickListener(operationListener);
				holder.txtDelect.setOnClickListener(operationListener);
			}

			//点击多选框
			if(checkedChangeListener!=null){
				holder.checkBox.setOnCheckedChangeListener(checkedChangeListener);
			}

			//输入购买的数量，每次输入进行数据的设置
			if(textWatcher!=null) {
				holder.txtCount.addTextChangedListener(textWatcher);
			}

			final View tCount = holder.txtCount;

			holder.txtCount.addTextChangedListener(new TextWatcher() {
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {

				}
				@Override
				public void afterTextChanged(Editable s) {
					String content = s.toString();//获取文本输入框的实际内容
					if(content.length() == 0){
						content = "0";
					}
					int count = Integer.parseInt(content.trim());
					Object tag = tCount.getTag();
					if(tag!=null&&tag instanceof Integer){
						int pos = (Integer) tag;
						CollectItem CollectItem = CollectItems.get(pos);
						CollectItem.setCount(count);
					}
				}
			});
		}

		CollectItem CollectItem = CollectItems.get(position);

		holder.txtDelect.setTag(position);
		holder.btnInc.setTag(position);
		holder.btnDec.setTag(position);
		holder.checkBox.setTag(position);
		holder.txtCount.setTag(position);

		holder.imaIcon.setImageResource(R.drawable.ic_launcher);
		holder.txtTitle.setText(CollectItem.getProductName());
		holder.txtPrice.setText(String.valueOf(CollectItem.getProductPrice()));
		holder.txtCount.setText(CollectItem.getCount()+"");
		if(CollectItem.isChecked()) {
			holder.checkBox.setChecked(true);
		}else {
			holder.checkBox.setChecked(false);
		}
		return view;
	}

	private static class ViewHolder{
		public CheckBox checkBox;
		public ImageView imaIcon;
		public TextView txtTitle;
		public TextView txtPrice;
		public TextView txtCount;
		public TextView txtDelect;
		public Button btnInc;
		public Button btnDec;


	}
}
